﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace SceneLib
{
    public class SceneMaterial
    {
        public Bitmap TextureImage;
        private IntPtr ptrBitmap;

        public Bitmap BumpImage;
        private IntPtr ptrBumpBitmap;

        public Bitmap NormalImage;
        private IntPtr ptrNormalBitmap;

        public string Name { get; set; }
        public string TextureFile { get; set; }
        public string BumpFile { get; set; }
        public string NormalFile { get; set; }
        public Vector Diffuse { get; set; }
        public Vector Specular { get; set; }
        public Vector Transparent { get; set; }
        public Vector Reflective { get; set; }
        public Vector RefractionIndex { get; set; }
        public float Shininess { get { return Specular.w; } set { Specular.w = value; } }
        public IntPtr PtrBitmap { get { return ptrBitmap; } }
        public IntPtr PtrBumpBitmap { get { return ptrBumpBitmap; } }
        public IntPtr PtrNormalBitmap { get { return ptrNormalBitmap; } }

        public SceneMaterial()
        {
            Specular = new Vector(0, 0, 0);
            Diffuse = new Vector(0, 0, 0);
            Reflective = new Vector(0, 0, 0);
        }

        public Vector GetTexturePixelColor(int x, int y)
        {
            if (TextureImage == null)
                return null;

            if (x < 0)
                x = TextureImage.Width + x;
            if (y < 0)
                y = TextureImage.Height + y;
            
            x %= TextureImage.Width;
            y %= TextureImage.Height;

            Color c = TextureImage.GetPixel(x, y);
            Vector color = new Vector(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f, c.A / 255.0f);
            return color;
        }

        public void CreatePointer()
        {
            BitmapData data = this.TextureImage.LockBits(new Rectangle(0, 0, TextureImage.Height, TextureImage.Width),
            ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            ptrBitmap = data.Scan0;

            this.TextureImage.UnlockBits(data);

        }       
    }
}
